import os
# 要搜索和替换的字符串
search_string = 'old_string'
replace_string = 'new_string'
# 指定目录路径
directory_path = '/path/to/your/markdown/files'
# 遍历目录及其子目录
for root, dirs, files in os.walk(directory_path):
for filename in files:
if filename.endswith('.md'):
# 构建文件的完整路径
file_path = os.path.join(root, filename)
# 读取文件内容
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 替换字符串
new_content = content.replace(search_string, replace_string)
# 写入新内容到文件
with open(file_path, 'w', encoding='utf-8') as file:
file.write(new_content)
print('所有 .md 文件中的替换已完成。')